home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / DistributedEO / DEOServer.tproj / DEOServer.m < prev    next >
Encoding:
Text File  |  1995-02-17  |  3.9 KB  |  141 lines

  1. /*
  2.    DEOServer.m created by enoyau on Fri 13-Jan-1995
  3.  
  4.    You may freely copy, distribute, and reuse the code in this example.
  5.    NeXT disclaims any warranty of any kind, expressed or implied, as to its
  6.    fitness for any particular use.
  7. */
  8.  
  9. #import "DEOServer.h"
  10.  
  11. #import <foundation/NSArray.h>
  12. #import <foundation/NSUtilities.h>
  13. #import <foundation/NSAutoreleasePool.h>
  14. #import <foundation/NSException.h>
  15. #import <remote/NXConnection.h>
  16. #import <remote/NXProxy.h>
  17.  
  18. @implementation DEOServer
  19.  
  20. - init
  21. {
  22.     [super init];
  23.     if(wantLog) NSLog(@"DEOServer: init");
  24.     clientDict  = [NSMutableDictionary new];
  25.     return self;
  26. }
  27.  
  28. - (void)dealloc
  29. {
  30.     if(wantLog) NSLog(@"DEOServer: dealloc");
  31.     [clientDict release];
  32.     [super dealloc];
  33. }
  34.  
  35. - (oneway)dispatchInformation:(NSDictionary *)info
  36.                        forKey:(NSString *)key
  37.                      userInfo:(NSDictionary *)userInfo
  38. {
  39.     NSMutableArray *array       = [clientDict objectForKey:key];
  40.     NSMutableArray *deadClients = [NSMutableArray array];
  41.     NSEnumerator   *enumerator;
  42.     id <DEOClient> next;
  43.  
  44.     if(wantLog)
  45.         NSLog(@"DEOServer: Receive information '%@' forKey '%s' user info '%@'",
  46.               [info description],
  47.               [key cString],
  48.               [userInfo description]);
  49.     if(wantLog) NSLog(@"           %d clients to notify", [array count]);
  50.  
  51.     enumerator = [array objectEnumerator];
  52.     while(next = [enumerator nextObject]) {
  53.         NS_DURING
  54.             [next dispatchInformation:info forKey:key userInfo:userInfo];
  55.         NS_HANDLER
  56.             [deadClients addObject:next];
  57.         NS_ENDHANDLER
  58.     }
  59.  
  60.     if(wantLog)
  61.         NSLog(@"           %d deads clients detected", [deadClients count]);
  62.     enumerator = [deadClients objectEnumerator];
  63.     while(next = [enumerator nextObject]) {
  64.         [self unregisterClient:next];
  65.     }
  66.     if(wantLog) NSLog(@"---------");
  67.     return;
  68. }
  69.  
  70. - (oneway)registerClient:(id <DEOClient>)client forKey:(NSString *)key
  71. {
  72.     NSMutableArray *array = [clientDict objectForKey:key];
  73.  
  74.     if(wantLog) NSLog(@"DEOServer: register client %x for key '%s'\n",
  75.                       (unsigned int)client,
  76.                       [key cString]);
  77.  
  78.     // A retainCount of 1 means that the client has not been registered
  79.     // before. The client is an autoreleased proxy of a subclass of NSObject.
  80.     if([(NSObject *)client retainCount] == 1) {
  81.         if(wantLog) NSLog(@"           DO registration");
  82.         [(NXProxy *)client setProtocolForProxy:@protocol(DEOClient)];
  83.     }
  84.  
  85.     if(!array) {
  86.         if(wantLog) NSLog(@"           first client for that key");
  87.         array = [NSMutableArray array];
  88.         [clientDict setObject:array forKey:key];
  89.     }
  90.  
  91.     // retainCount is incremented by 1 with this operation
  92.     [array addObject:client];
  93.  
  94.     if(wantLog) NSLog(@"---------");
  95.     return;
  96. }
  97.  
  98. - (oneway)unregisterClient:(id <DEOClient>)client forKey:(NSString *)key
  99. {
  100.     NSMutableArray *array = [clientDict objectForKey:key];
  101.  
  102.     if(wantLog) NSLog(@"DEOServer: unregister client %x for key '%s'\n",
  103.                       (unsigned int)client,
  104.                       [key cString]);
  105.  
  106.     if(array) {
  107.         [array removeObjectIdenticalTo:client]; // To avoid isEqual: on the wire.
  108.         if(![array count]) {
  109.             if(wantLog) NSLog(@"           last client for that key");
  110.             [clientDict removeObjectForKey:key];
  111.         }
  112.     }
  113.  
  114.     if(wantLog) NSLog(@"---------");
  115.     return;
  116. }
  117.  
  118. - (oneway)unregisterClient:(id <DEOClient>)client
  119. {
  120.     NSArray *keys = [clientDict allKeys];
  121.     NSEnumerator *enumerator = [keys objectEnumerator];
  122.     NSString *nextKey;
  123.  
  124.     if(wantLog)
  125.         NSLog(@"DEOServer: unregister client %x\n", (unsigned int)client);
  126.  
  127.     while(nextKey = [enumerator nextObject]) {
  128.         [self unregisterClient:client forKey:nextKey];
  129.     }
  130.     if(wantLog) NSLog(@"---------");
  131.  
  132.     return;
  133. }
  134.  
  135. - (BOOL)hasClient
  136. {
  137.     return [clientDict count] ? YES : NO;
  138. }
  139.  
  140. @end
  141.